﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ProductionSystem.RepairOrderDto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.ProductionSystem
{
    /// <summary>
    /// 维修单Services
    /// </summary>
    public class RepairOrderServices : ApplicationService, IRepairOrderServices
    {
        /// <summary>
        /// 维修单仓储
        /// </summary>
        private readonly IRepository<RepairOrderModelS, int> _repairOrderServies;
        
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="repairOrderServies">维修单仓储</param>
        public RepairOrderServices(IRepository<RepairOrderModelS, int> repairOrderServies)
        {
            _repairOrderServies = repairOrderServies;
        }

        /// <summary>
        /// 添加维修单
        /// </summary>
        /// <param name="dto">维修单信息</param>
        /// <returns></returns>
        public async Task<int> CreateRepairOrder(QueryRepairOrderDto dto)
        {
            var entity = ObjectMapper.Map<QueryRepairOrderDto, RepairOrderModelS>(dto);
            var result = await _repairOrderServies.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 查询维修单（分页/条件）
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns></returns>
        public async Task<RepairOrderQueryResult> QueryRepairOrder(GetRepairOrderDto dto)
        {
            var repairOrderList = await _repairOrderServies.GetListAsync();
            
            // 维修单编号查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.RepairOrderCode), t => t.RepairOrderCode.Contains(dto.RepairOrderCode)).ToList();
            
            // 维修单名称查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.RepairOrderName), t => t.RepairOrderName.Contains(dto.RepairOrderName)).ToList();
            
            // 设备编码查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.EquipmentCode), t => t.EquipmentCode.Contains(dto.EquipmentCode)).ToList();
            
            // 设备名称查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.EquipmentName), t => t.EquipmentName.Contains(dto.EquipmentName)).ToList();
            
            // 维修结果查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.RepairResults), t => t.RepairResults.Contains(dto.RepairResults)).ToList();
            
            // 单据状态查询
            repairOrderList = repairOrderList.WhereIf(!string.IsNullOrEmpty(dto.DocumentStatus), t => t.DocumentStatus.Contains(dto.DocumentStatus)).ToList();

            // 获取总记录数
            var totalCount = repairOrderList.Count;
            
            // 分页处理
            var pagedList = repairOrderList.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            var mappedList = ObjectMapper.Map<List<RepairOrderModelS>, List<QueryRepairOrderDto>>(pagedList);

            return new RepairOrderQueryResult
            {
                Data = mappedList,
                TotalCount = totalCount
            };
        }

        /// <summary>
        /// 修改维修单
        /// </summary>
        /// <param name="dto">维修单信息</param>
        /// <returns></returns>
        public async Task<int> UpdateRepairOrder(QueryRepairOrderDto dto)
        {
            var entity = ObjectMapper.Map<QueryRepairOrderDto, RepairOrderModelS>(dto);
            var result = await _repairOrderServies.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除维修单
        /// </summary>
        /// <param name="ids">维修单ID列表</param>
        /// <returns></returns>
        public async Task BatchDeleteRepairOrder(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return;
            await _repairOrderServies.DeleteDirectAsync(t => ids.Contains(t.Id));
        }

        /// <summary>
        /// 根据ID获取维修单信息
        /// </summary>
        /// <param name="id">维修单ID</param>
        /// <returns></returns>
        public async Task<QueryRepairOrderDto> GetRepairOrderById(int id)
        {
            var entity = await _repairOrderServies.GetAsync(id);
            return ObjectMapper.Map<RepairOrderModelS, QueryRepairOrderDto>(entity);
        }

        /// <summary>
        /// 根据ID获取维修单信息修改单据状态
        /// </summary>
        /// <param name="id">维修单ID</param>
        /// <returns></returns>
        public async Task<int> GetRepairOrderStatus(int id)
        {
            var entity = await _repairOrderServies.GetAsync(id);
            entity.DocumentStatus = "1"; // 假设状态改为"1"表示已完成
            var result = await _repairOrderServies.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
